package marcus.jpa;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import marcus.jpa.entities.Contact;
import marcus.jpa.entities.ContactId;
import marcus.jpa.entities.User;
import marcus.jpa.hibernate.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class SetUp {
	public static void main( String[] args )
	{
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.openSession();
		session.beginTransaction();
		session.createQuery("delete from Contact").executeUpdate();
		session.createQuery("delete from User").executeUpdate();

		User user = new User("1","Marcus","marcus.aidley@gmail.com");
		session.save(user);
		user = new User("2","Nia","rania.aidley@gmail.com");
		session.save(user);

		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
		Contact contact;
		try {
			contact = new Contact(new ContactId("1","Philip"), s.parse("1973-03-28"));
			session.save(contact);
			contact = new Contact(new ContactId("1","Nia"), s.parse("1977-10-12"));
			session.save(contact);
			contact = new Contact(new ContactId("2","Marcus"), s.parse("1975-04-17"));
			session.save(contact);
			contact = new Contact(new ContactId("2","Zoe"), s.parse("2012-07-06"));
			session.save(contact);
			contact = new Contact(new ContactId("2","Amy"), s.parse("2012-07-06"));
			session.save(contact);
			contact = new Contact(new ContactId("2","William"), s.parse("2011-01-24"));
			session.save(contact);
			contact = new Contact(new ContactId("2","Rose"), s.parse("2008-10-24"));
			session.save(contact);
			contact = new Contact(new ContactId("2","Daisy"), s.parse("2005-10-27"));
			session.save(contact);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		session.getTransaction().commit();

		session.close();
	}
}
